package com.xbongbong.lfasr.worker;

import com.xbongbong.lfasr.file.ChannelFileReader;
import com.xbongbong.lfasr.model.UploadParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;

public class UploadThread implements Runnable {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadThread.class);
    private UploadParams params;
    private int file_piece_size;

    public UploadThread(UploadParams params, int file_piece_size) {
        this.params = params;
        this.file_piece_size = file_piece_size;
    }

    @Override
    public void run() {
        try {
            ChannelFileReader fr = new ChannelFileReader(this.params.getFile(), this.file_piece_size);
            SliceWorker sw = new SliceWorker(this.params, (long)this.file_piece_size, false, new HashMap());
            sw.sliceFile(fr);
            fr.close();

            for(boolean isSend = sw.getEventHandler().isSendAll(); !isSend; isSend = sw.getEventHandler().isSendAll()) {
                try {
                    Thread.sleep(500L);
                } catch (Exception var5) {
                    ;
                }
            }

            LOGGER.info(String.format("[COMPENT]-%s [PROCESS]-%s [ID]-%s [STATUS]-%s [MEASURE]-%s [DEF]-%s", "CLIENT", "UploadThread", this.params.getTaskId(), "", "(-1) ms", "upload file send success, file:" + this.params.getFile().getAbsolutePath() + ", task_id:" + this.params.getTaskId()));
            sw.setFileEnd();
            sw.getEventHandler().await();
            sw.getEventHandler().shutdownNow();
        } catch (Exception var6) {
            LOGGER.error(String.format("[COMPENT]-%s [PROCESS]-%s [ID]-%s [STATUS]-%s [MEASURE]-%s [DEF]-%s", "CLIENT", "UploadThread", this.params.getTaskId(), "", "(-1) ms", "upload file send error, file:" + this.params.getFile().getAbsolutePath() + ", task_id:" + this.params.getTaskId()), var6);
        }

    }
}
